﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Runtime.Serialization;
using System.Web.Script.Serialization;
using System.Data.SqlClient;
using CMD_WEB.DAL;
using Newtonsoft.Json;

namespace CMD.BAL.SOL
{
    [DataContractAttribute]
    public class SolicitudDocs
    {
        [DataMemberAttribute(EmitDefaultValue = false)]
        public string parametrosDocs;
        public Dictionary<string, string> parametros;
        public static string InsertDocs(string parametrosDocs)
        {
            DataSet result = new DataSet();
            SqlCommand sp = new SqlCommand("SP_CMD_INSERT_SOL_DOCS");
            sp.CommandType = CommandType.StoredProcedure;
            Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(parametrosDocs);
            //UpsertSolicitudWeb solweb= null;
            long mensaje = 0;
            long mensaje2 = 0;
            string resultado = "";
            try
            {
                foreach (KeyValuePair<string, string> d in values)
                {
                    if (d.Key != "doctos_sync")
                    {
                        if (d.Key != "doctos_fileDateLoaded")
                            sp.Parameters.AddWithValue(d.Key, d.Value);
                        else
                            sp.Parameters.AddWithValue(d.Key, Convert.ToDateTime(d.Value.ToString()).ToString("yyyyMMdd hh:mm:ss"));
                    }
                }
                result = SrvDB.ExecuteCommand(sp);
                if (result.Tables.Count == 1)
                {
                    mensaje = Convert.ToInt64(result.Tables[0].Rows[0]["doctos_unidDoctos"]);
                    mensaje2 = Convert.ToInt64(result.Tables[0].Rows[0]["doctos_unidDoc"]);
                    resultado = "doctos_unidDoctos: " + mensaje.ToString() + "," + "doctos_unidDoc: " + mensaje2.ToString();
                    
                }
                //solweb = new UpsertSolicitudWeb(Convert.ToString(result.Tables[0].Rows[0]["unidSolicitud"]));
                if (result.Tables.Count == 0)
                    resultado = "No se guardaron los datos";
            }

            catch (Exception ex)
            {
                throw ex;
            }
            //return  solweb.unidSolicitud.ToString();
            return resultado;
        }

        public SolicitudDocs()
        {
            parametrosDocs = "";
        }
    }
}
